﻿using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using Microsoft.Win32;
using System.Windows.Forms;

/// <summary>
/// An useful class to read/write/delete/count registry keys
/// </summary>
public class ModifyRegistry
{
    private bool m_showError = false;
    /// <summary>
    /// A property to show or hide error messages 
    /// (default = false)
    /// </summary>
    public bool ShowError
    {
        get { return m_showError; }
        set { m_showError = value; }
    }

    private string m_subKey = "SOFTWARE\\" + Application.ProductName.ToUpper();
    /// <summary>
    /// A property to set the SubKey value
    /// (default = "SOFTWARE\\" + Application.ProductName.ToUpper())
    /// </summary>
    public string SubKey
    {
        get { return m_subKey; }
        set { m_subKey = value; }
    }

    private RegistryKey m_baseRegistryKey = Registry.LocalMachine;
    /// <summary>
    /// A property to set the BaseRegistryKey value.
    /// (default = Registry.LocalMachine)
    /// </summary>
    public RegistryKey BaseRegistryKey
    {
        get { return m_baseRegistryKey; }
        set { m_baseRegistryKey = value; }
    }

    // **************************************************************************
    //		 * *************************************************************************


    /// <summary>
    /// To read a registry key.
    /// input: KeyName (string)
    /// output: value (string) 
    /// </summary>
    public string Read(string KeyName)
    {
        // Opening the registry key
        RegistryKey rk = m_baseRegistryKey;
        // Open a subKey as read-only
        RegistryKey sk1 = rk.OpenSubKey(m_subKey);
        // If the RegistrySubKey doesn't exist -> (null)
        if (sk1 == null)
        {
            return null;
        }
        else
        {
            try
            {
                // If the RegistryKey exists I get its value
                // or null is returned.
                return (string)sk1.GetValue(KeyName.ToUpper());
            }
            catch (Exception e)
            {
                // AAAAAAAAAAARGH, an error!
                ShowErrorMessage(e, "Reading registry " + KeyName.ToUpper());
                return null;
            }
        }
    }

    // **************************************************************************
    //		 * *************************************************************************


    /// <summary>
    /// To write into a registry key.
    /// input: KeyName (string) , Value (object)
    /// output: true or false 
    /// </summary>
    public bool Write(string KeyName, object Value)
    {
        try
        {
            // Setting
            RegistryKey rk = m_baseRegistryKey;
            // I have to use CreateSubKey 
            // (create or open it if already exits), 
            // 'cause OpenSubKey open a subKey as read-only
            RegistryKey sk1 = rk.CreateSubKey(m_subKey);
            // Save the value
            sk1.SetValue(KeyName.ToUpper(), Value);

            return true;
        }
        catch (Exception e)
        {
            // AAAAAAAAAAARGH, an error!
            ShowErrorMessage(e, "Writing registry " + KeyName.ToUpper());
            return false;
        }
    }

    // **************************************************************************
    //		 * *************************************************************************


    /// <summary>
    /// To delete a registry key.
    /// input: KeyName (string)
    /// output: true or false 
    /// </summary>
    public bool DeleteKey(string KeyName)
    {
        try
        {
            // Setting
            RegistryKey rk = m_baseRegistryKey;
            RegistryKey sk1 = rk.CreateSubKey(m_subKey);
            // If the RegistrySubKey doesn't exists -> (true)
            if (sk1 == null)
            {
                return true;
            }
            else
            {
                sk1.DeleteValue(KeyName);
            }

            return true;
        }
        catch (Exception e)
        {
            // AAAAAAAAAAARGH, an error!
            ShowErrorMessage(e, Convert.ToString("Deleting SubKey ") + m_subKey);
            return false;
        }
    }

    // **************************************************************************
    //		 * *************************************************************************


    /// <summary>
    /// To delete a sub key and any child.
    /// input: void
    /// output: true or false 
    /// </summary>
    public bool DeleteSubKeyTree()
    {
        try
        {
            // Setting
            RegistryKey rk = m_baseRegistryKey;
            RegistryKey sk1 = rk.OpenSubKey(m_subKey);
            // If the RegistryKey exists, I delete it
            if (sk1 != null)
            {
                rk.DeleteSubKeyTree(m_subKey);
            }

            return true;
        }
        catch (Exception e)
        {
            // AAAAAAAAAAARGH, an error!
            ShowErrorMessage(e, Convert.ToString("Deleting SubKey ") + m_subKey);
            return false;
        }
    }

    // **************************************************************************
    //		 * *************************************************************************


    /// <summary>
    /// Retrive the count of subkeys at the current key.
    /// input: void
    /// output: number of subkeys
    /// </summary>
    public int SubKeyCount()
    {
        try
        {
            // Setting
            RegistryKey rk = m_baseRegistryKey;
            RegistryKey sk1 = rk.OpenSubKey(m_subKey);
            // If the RegistryKey exists...
            if (sk1 != null)
            {
                return sk1.SubKeyCount;
            }
            else
            {
                return 0;
            }
        }
        catch (Exception e)
        {
            // AAAAAAAAAAARGH, an error!
            ShowErrorMessage(e, Convert.ToString("Retriving subkeys of ") + m_subKey);
            return 0;
        }
    }

    // **************************************************************************
    //		 * *************************************************************************


    /// <summary>
    /// Retrive the count of values in the key.
    /// input: void
    /// output: number of keys
    /// </summary>
    public int ValueCount()
    {
        try
        {
            // Setting
            RegistryKey rk = m_baseRegistryKey;
            RegistryKey sk1 = rk.OpenSubKey(m_subKey);
            // If the RegistryKey exists...
            if (sk1 != null)
            {
                return sk1.ValueCount;
            }
            else
            {
                return 0;
            }
        }
        catch (Exception e)
        {
            // AAAAAAAAAAARGH, an error!
            ShowErrorMessage(e, Convert.ToString("Retriving keys of ") + m_subKey);
            return 0;
        }
    }

    // **************************************************************************
    //		 * *************************************************************************


    private void ShowErrorMessage(Exception e, string Title)
    {
        if (m_showError == true)
        {
            MessageBox.Show(e.Message, Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}